package visual;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.JPanel;

import main.GameDef;

public class Board extends JPanel implements MouseListener, GameDef{
	public Rectangle castleArea = new Rectangle();
	public int id;
	public String name;
	public Vector<Piece> castle = new Vector<Piece>(0);
	
	public Board(){
		addMouseListener(this);
	}
	
	public void paint(Graphics g){
		g.setColor(new Color(0x000000));
		g.fillRect(0, 0, BOARD_WIDTH, BOARD_HEIGHT);
		if(castleArea != null){
			g.setColor(new Color(0xFF00FF));
			g.fillRect(castleArea.x, castleArea.y, castleArea.width, castleArea.height);
			for(int i = 0; i < castle.size(); i++){
				castle.get(i).paint(g);
			}
		}
		g.setColor(new Color(0xFFFFFF));
		for(int i = 0; i < BOARD_PIECES_Y; i++){
			g.drawLine(0, i * BLOCK_HEIGHT, BOARD_WIDTH, i * BLOCK_HEIGHT);
		}
		for(int i = 0; i < BOARD_PIECES_X; i++){
			g.drawLine(i * BLOCK_WIDTH, 0, i * BLOCK_WIDTH, BOARD_HEIGHT);
		}
	}
	
	public Dimension getPreferredSize(){
		return new Dimension(BLOCK_WIDTH * BOARD_WIDTH, BLOCK_WIDTH * BOARD_WIDTH);
	}
	
	public Dimension getMaximumSize(){
		return getPreferredSize();
	}
	
	public Dimension getMinimumSize(){
		return getPreferredSize();
	}
	
	public void mouseClicked(MouseEvent e) {
		if(e.getButton() == e.BUTTON1){
			if(FrameHandler.hasToPutCastle){
				FrameHandler.hasToPutCastle = false;
				setCastlePosition(e.getPoint());
				paint(getGraphics());
			}else if(FrameHandler.hasToPutPiece){
				FrameHandler.hasToPutPiece = false;
				castle.add(new Piece(FrameHandler.hasToPutPieceId, e.getPoint(), FrameHandler.hasToPutPieceVertical));
				paint(getGraphics());
			}
		}
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}
	
	public void setCastlePosition(Point pos){
		castleArea = new Rectangle(pos.x - (pos.x % BLOCK_WIDTH), pos.y - (pos.y % BLOCK_HEIGHT), CASTLE_WIDTH, CASTLE_HEIGHT);
	}
}
